package com.soilflow.web.controller.bigdata;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soilflow.common.constant.UserConstants;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.domain.LandAuction;
import com.soilflow.common.domain.LandClassify;
import com.soilflow.common.domain.bigdata.LandBigdataDto;
import com.soilflow.common.domain.bigdata.LandCountGroupByStatus;
import com.soilflow.common.domain.bigdata.LandCountVO;
import com.soilflow.common.domain.dto.LandClassifyDto;
import com.soilflow.common.domain.vo.LandClassifyVo;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.annotation.Log;
import com.soilflow.common.utils.poi.ExcelUtil;
import com.soilflow.framework.web.domain.server.Sys;
import com.soilflow.info.service.IBigdataService;
import com.soilflow.info.service.LandClassifyService;
import com.soilflow.system.domain.SysTemplate;
import com.soilflow.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.soilflow.common.constant.LandConstants.LandStatus;
import com.soilflow.common.constant.LandConstants.LandUsage;

/**
 * 分类Controller
 *
 * @author summer
 * @date 2022-06-22
 */
@Api("分类设置")
@RestController
@RequestMapping("/bigdata")
public class LandController extends BaseController {
    @Autowired
    private LandClassifyService soilflowClassifyService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IBigdataService bigdataService;

    /**
     * 大通湖区的土地统计数据
     * @param dto
     * @return
     */
    @GetMapping("/county/data")
    public AjaxResult getCountyLandData(SysDept dto){
        SysDept rootDept = this.getRootDept();
        LandCountVO landCountVO = bigdataService.countDeptLand(rootDept.getDeptId());
        return AjaxResult.success(landCountVO);
    }

    /**
     * 乡镇列表
     * @return
     */
    @GetMapping("/town/list")
    public AjaxResult townList(SysDept dto) {
        SysDept rootDept = this.getRootDept();
        List<SysDept> townList = this.getSubLevelDept(rootDept.getDeptId());
        List<LandCountVO> townData = new ArrayList<>();
        for(int i=0; i<townList.size(); i++){
            LandCountVO item = bigdataService.countDeptLand(townList.get(i).getDeptId());
            item.setDeptName(townList.get(i).getDeptName());
            item.setDeptId(townList.get(i).getDeptId());
            item.setLongitude(townList.get(i).getLongitude());
            item.setLatitude(townList.get(i).getLatitude());
            townData.add(item);
        }
        return AjaxResult.success(townData);
    }

    private List<SysDept> getSubLevelDept(Long deptId){
        SysDept sysDept = new SysDept();
        sysDept.setParentId(deptId);
        sysDept.setStatus("0");
        return sysDeptService.selectAllDeptList(sysDept);
    }

    private SysDept getRootDept(){
        QueryWrapper<SysDept> rootQueryWrapper = new QueryWrapper<>();
        rootQueryWrapper.lambda().eq(SysDept::getParentId, 0L)
                .last("limit 1");
        return sysDeptService.getOne(rootQueryWrapper);
    }

    /**
     * 行政村列表
     * @return
     */
    @GetMapping("/village/list")
    public AjaxResult villageList() {

        // 第一步：查出一级组织
        SysDept rootDept = this.getRootDept();

        // 第二步：查出乡镇组织
        List<SysDept> secondLevelDept = this.getSubLevelDept(rootDept.getDeptId());

        // 第三步：查出村级组织
        List<SysDept> thirdLevelDept = new ArrayList<>();
        for(int i=0; i<secondLevelDept.size(); i++){
            List<SysDept> subLevelDept = this.getSubLevelDept(secondLevelDept.get(i).getDeptId());
            thirdLevelDept.addAll(subLevelDept);
        }

        // 查出每个村的土地数据
        List<LandCountVO> villageData = new ArrayList<>();
        for(int i=0; i<thirdLevelDept.size(); i++){
            LandCountVO item = bigdataService.countDeptLand(thirdLevelDept.get(i).getDeptId());
            item.setDeptName(thirdLevelDept.get(i).getDeptName());
            item.setDeptId(thirdLevelDept.get(i).getDeptId());
            item.setLongitude(thirdLevelDept.get(i).getLongitude());
            item.setLatitude(thirdLevelDept.get(i).getLatitude());
            villageData.add(item);
        }
        return AjaxResult.success(villageData);
    }

    /**
     * 租赁数据统计
     * @return
     */
    @GetMapping("/hire/data")
    public AjaxResult getHireData(LandBigdataDto dto) {
        return AjaxResult.success(bigdataService.countLandData(dto));
    }

    /**
     * 租赁数据统计(按月分组统计)
     * @return
     */
    @GetMapping("/hire/monthdata")
    public AjaxResult getHireDataGroupByMonth(LandBigdataDto dto) {
        return AjaxResult.success(bigdataService.countLandDataGroupByMonth(dto));
    }






}